package com.project_doctor.controller;

import com.project_doctor.pojo.Conversation;
import com.project_doctor.pojo.Message;
import com.project_doctor.service.ConversationService;
import com.project_doctor.service.DoctorService;
import com.project_doctor.service.MessageService;
import com.project_doctor.util.JwtUtil;
import io.minio.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("message")
public class MessageController {
    @Resource
    private MessageService messageService;

    @Autowired
    private ConversationService conversationService;
    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    DoctorService doctorService;
    /**
     * 获取会话消息列表
     */
    @GetMapping("/getMessagesByConversationId/{conversationId}")
    public ResponseEntity<?> getMessagesByConversationId(
            @PathVariable Long conversationId) {

            List<Message> messages = messageService.getMessagesByConversationId(conversationId);
            return ResponseEntity.ok(messages);

    }

    /**
     * 发送消息
     */
    @PostMapping("/saveMessage")
    public ResponseEntity<?> saveMessage(
            @RequestBody Message message) {
        Message savedMessage = messageService.saveMessage(message);
        return ResponseEntity.ok(savedMessage);
    }

    @GetMapping("/getUnreadMessages/{conversationId}/{userId}")
    public ResponseEntity<?> getUnreadMessages(@PathVariable Long conversationId, @PathVariable Long userId) {
        Integer i = messageService.calculateUnreadCount(conversationId, userId);
        return ResponseEntity.ok(i);
    }
}
